package com.soft.service;

import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.User;
import com.soft.entity.Purchase;
import com.soft.mapper.PurchaseMapper;
import com.soft.mapper.UserMapper;

@Service
public class PurchaseService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;
    public List<Purchase> queryByUserNamePurchaseTotle(String userName) {
	    QueryWrapper<User> userWrapper = new QueryWrapper<>();
	    userWrapper.eq("user_name", userName);
		User user = userMapper.selectOne(userWrapper);
	    QueryWrapper<Purchase> purchaseWrapper = new QueryWrapper<>();
	    purchaseWrapper.eq("user_id", user.getUserId());
	    return purchaseMapper.selectList(purchaseWrapper);
	}
    public int insertPurchase(String purchaseGoods,String purchaseTotle,String userName) {
    	QueryWrapper<User> userWrapper = new QueryWrapper<>();
    	userWrapper.eq("user_name",userName);
    	User user = userMapper.selectOne(userWrapper);
    	Purchase purchase = new Purchase();
    	purchase.setPurchaseGoods(purchaseGoods);
    	purchase.setPurchaseTotle(purchaseTotle);
    	purchase.setUserId(user.getUserId());
    	int num = purchaseMapper.insert(purchase);
    	return num;
    }
    public int updatePurchase(String purchaseGoods,String purchaseTotle,String userName) {
    	QueryWrapper<User> userWrapper = new QueryWrapper<>();
    	userWrapper.eq("user_name",userName);
    	User user = userMapper.selectOne(userWrapper);
    	UpdateWrapper<Purchase> purchaseWrapper = new UpdateWrapper<>();
        purchaseWrapper.eq("purchase_goods", purchaseGoods) // 通过外键关联用户
        .eq("user_id", user.getUserId());
    	Purchase purchase = new Purchase();
    	purchase.setPurchaseTotle(purchaseTotle);
    	int num = purchaseMapper.update(purchase,purchaseWrapper);
    	return num;
    }
}
